/**
 * ClassName:AdminKeyServiceImpl
 * Package:com.cskaoyan.market.service
 * Description:
 *
 * @Author LY
 * @Create 2024/6/19 21:18
 * @Version1.0
 **/

package com.cskaoyan.market.service.impl;

import com.cskaoyan.market.db.domain.MarketCoupon;
import com.cskaoyan.market.db.domain.MarketKeyword;
import com.cskaoyan.market.db.domain.MarketKeywordExample;
import com.cskaoyan.market.db.mapper.MarketKeywordMapper;
import com.cskaoyan.market.service.AdminKeyService;
import com.cskaoyan.market.util.MybatisUtils;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.javassist.compiler.ast.Keyword;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class AdminKeyServiceImpl implements AdminKeyService {

    @Override
    public List<MarketKeyword> list(String page, String limit, String keyword, String url, String sort, String order) {
        SqlSession session = MybatisUtils.getSession();
        MarketKeywordMapper marketKeywordMapper = session.getMapper(MarketKeywordMapper.class);
        MarketKeywordExample marketKeywordExample = new MarketKeywordExample();
        MarketKeywordExample.Criteria criteria = marketKeywordExample.createCriteria();
        if(!StringUtils.isEmpty(keyword))
        {
            criteria.andKeywordLike("%"+keyword+"%");
        }
        if (!StringUtils.isEmpty(url))
        {
            criteria.andKeywordEqualTo(url);
        }
        PageHelper.startPage(Integer.parseInt(page),Integer.parseInt(limit));
        List<MarketKeyword> marketKeywords = marketKeywordMapper.selectByExample(marketKeywordExample);
        session.close();
        return marketKeywords;
    }

    @Override
    public int create(MarketKeyword marketKeyword) {
        SqlSession session = MybatisUtils.getSession();
        MarketKeywordMapper marketKeywordMapper = session.getMapper(MarketKeywordMapper.class);

        int effectRowNum = marketKeywordMapper.insertSelective(marketKeyword);
        session.commit();
        session.close();
        return effectRowNum;
    }

    @Override
    public int delete(MarketKeyword marketKeyword) {
        SqlSession session = MybatisUtils.getSession();
        int effectRowNum = session.getMapper(MarketKeywordMapper.class).deleteByPrimaryKey(marketKeyword.getId());
        session.commit();
        session.close();
        return effectRowNum;
    }

    @Override
    public boolean update(MarketKeyword marketKeyword) {
        SqlSession session = MybatisUtils.getSession();
        MarketKeywordMapper mapper = session.getMapper(MarketKeywordMapper.class);
        int i = mapper.updateByPrimaryKeySelective(marketKeyword);
        session.commit();
        session.close();
        return true;
    }
}
